package cn.edu.xust.communication.remote;

import cn.edu.xust.configuration.exception.Default200Exception;
import cn.edu.xust.entity.AmmeterParameterEntity;
import cn.edu.xust.communication.AbstractAmmeterReaderWriterAdapter;
import cn.edu.xust.communication.AmmeterAutoReader;
import cn.edu.xust.communication.model.Result;
import cn.edu.xust.communication.protocol.Dlt645Frame;
import cn.edu.xust.communication.server.NettyServer;
import cn.edu.xust.communication.server.cache.ChannelMap;
import cn.edu.xust.communication.util.Dlt645FrameUtils;
import cn.edu.xust.communication.util.FileUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 电表遥测实现类
 *
 * @author ：huangxin
 * @modified ：
 * @since ：2020/03/26 10:33
 */
@Component
public class AmmeterRemoteReaderImpl extends AbstractAmmeterReaderWriterAdapter {




    @Override
    public List<AmmeterParameterEntity> realTelemetry(List<String> deviceNumberList) {
        // 创建一个空的结果列表
        ArrayList<AmmeterParameterEntity> list = new ArrayList<>();

        // 遍历设备编号列表
        deviceNumberList.forEach(deviceNum -> {
            // 创建 AmmeterAutoReader 对象，用于读取电表数据
            AmmeterAutoReader reader = new AmmeterAutoReader(ChannelMap.getDeviceIp(deviceNum), deviceNum);

            // 创建 AmmeterParameter 对象，并设置设备编号
            AmmeterParameterEntity ammeterParameter = new AmmeterParameterEntity();
            ammeterParameter.setDeviceNumber(deviceNum);

            // 读取电表的电流参数，并设置到 AmmeterParameter 对象中
            try {
                ammeterParameter.setCurrentACurrent(reader.readCurrentIA());
                ammeterParameter.setCurrentBCurrent(reader.readCurrentIB());
                ammeterParameter.setCurrentCCurrent(reader.readCurrentIC());

                // 读取电表的电压参数，并设置到 AmmeterParameter 对象中
                ammeterParameter.setCurrentAVoltage(reader.readCurrentVA());
                ammeterParameter.setCurrentBVoltage(reader.readCurrentVB());
                ammeterParameter.setCurrentCVoltage(reader.readCurrentVC());

                // 读取电表的有功功率和无功功率参数，并设置到 AmmeterParameter 对象中
                ammeterParameter.setCurrentActivePower(reader.readCurrentActivePower());
                ammeterParameter.setCurrentReactivePower(reader.readCurrentReactivePower());

                // 读取电表的功率因数参数，并设置到 AmmeterParameter 对象中
                ammeterParameter.setCurrentPowerFactor(reader.readCurrentPowerFactor());
            } catch (Default200Exception e) {
                throw new RuntimeException(e);
            }
            // 将 AmmeterParameter 对象添加到结果列表中
            list.add(ammeterParameter);

        });

        // 返回结果列表
        return list;
    }


    @Override
    public List<AmmeterParameterEntity> realRegion(List<String> deviceNumberList) {
        ArrayList<AmmeterParameterEntity> list = new ArrayList<>();
        deviceNumberList.forEach(deviceNum-> {
            AmmeterAutoReader reader = new AmmeterAutoReader(ChannelMap.getDeviceIp(deviceNum), deviceNum);
            AmmeterParameterEntity ammeterParameter = new AmmeterParameterEntity();
            try {
                ammeterParameter.setCurrentTotalActivePower( reader.readCurrentTotalActiveEnergy());
            } catch (Default200Exception e) {
                throw new RuntimeException(e);
            }
            list.add(ammeterParameter);
        });
        return list;
    }







}
